package com.branches.chapeter_4;

import java.util.concurrent.TimeUnit;

public class Comsumer implements Runnable {
    String name;
    Product p;

    public Comsumer(Product p, String name) {
        this.p = p;
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this.p) {
                while (this.p.getCount() <= 0) {
                    try {
                        this.p.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("before: Comsumer name = " + this.name + ", p count = " + p.getCount());
                this.p.reduce();
                System.out.println("after: Comsumer name = " + this.name + ", p count = " + p.getCount());
            }

            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}